import java.io.IOException;
import java.io.InputStream;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;    
import java.io.FileInputStream;
import java.io.FileOutputStream;    
 
 
 
public class FtpFile {
 
	private static final Log log = LogFactory.getLog(FtpFile.class);
	
	private FTPClient fc;
 
	private String ip = "39.96.33.164";
	private int port = 21;
	private String userName = "ejiao";
 
	private String password = "123456";
 
	public FtpFile() {
		super();
	}
 
	public FtpFile(String ip, int port, String userName, String password) throws Exception {
		super();
		this.ip = ip;
		this.port = port;
		this.userName = userName;
		this.password = password;
		this.connectServer();
	}
 
	/**
	 * 连接到ftp
	 * @param ip
	 *            FTP的IP地址
	 * @param port
	 *            FTP的端口号
	 * @param userName
	 *            登录FTP地址的用户名
	 * @param password
	 *            登录FTP地址的密码
	 * @throws Exception 
	 */
	public void connectServer() throws Exception {
		try {
			fc = new FTPClient();
			if (port == 0) {
				port = 21;
			}
			fc.connect(ip, port);
			fc.login(userName, password);
			fc.setFileType(FTPClient.BINARY_FILE_TYPE);
			log.info("成功连接ftp服务器：");
			System.out.println("成功连接ftp服务器："+ip);
		} catch (Exception e) {
			log.error("ftp服务器连接失败",e);
			throw new Exception(e);
		}
	}
	
    /**  
     *   
     * @param file 上传的文件或文件夹  
     * @throws Exception  
     */    
	public void uploadForder(File forder) throws Exception{		
        if(forder.isDirectory()){           
        	fc.makeDirectory(forder.getName());                
        	fc.changeWorkingDirectory(forder.getName());      
            String[] files = forder.list();             
            for (int i = 0; i < files.length; i++) {      
                File file1 = new File(forder.getPath()+System.getProperty("file.separator")+files[i] );      
                if(file1.isDirectory()){      
                	uploadForder(file1);      
                    fc.changeToParentDirectory();      
                }else{                    
                    File file2 = new File(forder.getPath()+System.getProperty("file.separator")+files[i]);      
                    FileInputStream input = new FileInputStream(file2);      
                    fc.storeFile(file2.getName(), input);      
                    input.close();                            
                }                 
            }      
        }else{      
            File file2 = new File(forder.getPath());      
            FileInputStream input = new FileInputStream(file2);      
            fc.storeFile(file2.getName(), input);      
            input.close();        
        }      
    }     	
 
	/**
	 * 上传文件到ftp
	 * 
	 * @param in
	 *            本地文件
	 * @param remoteFile
	 *            服务器上的文件名
	 * @throws Exception 
	 */
	public void uploadFile(InputStream in, String remoteFile) throws Exception {
		try {
		     System.out.print(fc.getReplyString());
		    Integer reply = fc.getReplyCode();  
	        if (!FTPReply.isPositiveCompletion(reply)) {  
	            fc.disconnect();  
	            return;  
	        }  
 
			fc.setFileType(FTPClient.BINARY_FILE_TYPE);
			fc.storeUniqueFile(remoteFile, in);
		} catch (IOException e) {
			log.error("文件上传失败  上传到服务器的文件名：" + remoteFile,e);
			throw new Exception(e);
		}finally{
			if(in != null){
				in.close();
			}
		}
	}
 
	/**
	 * 下载ftp上的文件
	 * 
	 * @param outFileStream
	 *            下载到本地的文件流
	 * @param remoteFile
	 *            服务器上的文件名
	 * @throws Exception 
	 * 
	 */
	public void downLoadFile(FileOutputStream outFileStream, String remoteFile) throws Exception {
		try {
			fc.retrieveFile(remoteFile, outFileStream);
		} catch (IOException e) {
			log.error("文件下载失败  下载到服务器的文件名：" + remoteFile,e);
			throw new Exception(e);
		}
	}
	
	/**
	 * 删除ftp上的文件
	 * 
	 * @param url
	 *            要删除的文件名
	 * @throws Exception 
	 */
	public boolean deleteFile(String url) throws Exception {
		int status = 0;
		try {
			status = fc.sendCommand(url);
			log.info("远程执行命令： " + url + "\r\n");
			return true;
		} catch (IOException e) {
			log.error("远程执行命令失败：" + url,e);
			throw new Exception(e);
		}finally{
			if (status == 250) {
				return true;
			}
		}
	}
 
	/**
	 * 关闭FTP服务
	 * @throws Exception 
	 */
	public void closeServer() throws Exception {
		try {
			fc.logout();
		} catch (IOException e) {
			log.error("关闭ftp连接时出错：",e);
			throw new Exception(e);
		} finally {
			if (fc.isConnected()) {
				fc.disconnect();
			}
 
		}
	}
	
	/**
	 * 创建下载或者阅读时的ftp Url
	 * @return
	 */
	/*public static String createUrl(Integer bizauth){
		FtpFile ftpFile = new FtpFile();
		StringBuffer url = new StringBuffer("ftp://");
		if(bizauth == BizAuth.AUTH_ID_DOWNLOAD){
			url.append( ftpFile.getUserName() + ":" + ftpFile.getPassword() + "@" + ftpFile.getIp() + ":" + ftpFile.getPort() + "/");
		}else if(bizauth == BizAuth.AUTH_ID_LOOK){
			url.append( ftpFile.getIp() + ":" + ftpFile.getPort() + "/");
		}
		return url.toString();
	}*/
 
	public String getIp() {
		return ip;
	}
 
	public void setIp(String ip) {
		this.ip = ip;
	}
 
	public int getPort() {
		return port;
	}
 
	public void setPort(int port) {
		this.port = port;
	}
 
	public String getUserName() {
		return userName;
	}
 
	public void setUserName(String userName) {
		this.userName = userName;
	}
 
	public String getPassword() {
		return password;
	}
 
	public void setPassword(String password) {
		this.password = password;
	}
 
	public FTPClient getFc() {
		return fc;
	}
 
	public void setFc(FTPClient fc) {
		this.fc = fc;
	}
	public static void main(String[] args) {
 
		 String ip = "39.96.33.164";
		 int port = 21;
		 String userName = "ejiao";
 
		 String password = "123456";
 
		FtpFile ftp;
		try {
			ftp = new FtpFile(ip,port,userName,password);
		//	ftp.downLoadFile(soutFileStream,  remoteFile);
		
		System.out.println(ftp.getIp());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	//	ftp.downLoadFile(outFileStream, remoteFile)
	}
}
